package org.hbgl.geoentity.admin.web.admin;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hbgl.geoentity.admin.annotation.OperationLog;
import org.hbgl.geoentity.admin.annotation.RequiresPermissionsDesc;
import org.hbgl.geoentity.admin.aspect.OperationType;
import org.hbgl.geoentity.admin.shiro.UserType;
import org.hbgl.geoentity.admin.web.BaseController;
import org.hbgl.geoentity.core.util.JacksonUtil;
import org.hbgl.geoentity.core.util.ResponseUtil;
import org.hbgl.geoentity.db.domain.GeContent;
import org.hbgl.geoentity.db.domain.GeUser;
import org.hbgl.geoentity.db.domain.SiteSetting;
import org.hbgl.geoentity.db.util.EnumFile;
import org.hbgl.geoentity.db.vo.AnalyRecordVo;
import org.hbgl.geoentity.db.vo.ContentVo;
import org.hbgl.geoentity.db.vo.NodeApplyVo;
import org.hbgl.geoentity.db.vo.NodeVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.List;

import static org.hbgl.geoentity.admin.util.ResponseCode.ADMIN_NAME_EXIST;

/**
 * 代码功能：数据管理
 * 开发日期：2022/4/26
 * 作者：蒋海仑
 * 联系方式：
 * 版本更迭说明：
 * 版权信息：河北广联信息技术有限公司
 */
@RestController
@RequestMapping("/admin/data")
@Validated
public class AdminDataController extends BaseController {

    //region 数据管理--节点建设

    /**
     * 节点建设
     */
    @RequiresPermissions("admin:data:list")
    @RequiresPermissionsDesc(menu = {"数据管理", "节点建设"}, button = "查询")
    @GetMapping("/list")
    public Object list(@RequestParam(defaultValue = "-1") Integer columnId,
                       @RequestParam(defaultValue = "") String keywords,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit) {
        GeUser userCurrent = getCurrentAdminUser();
        List<ContentVo> contentList = contentService.selectContentList(columnId, keywords, page, limit);
        return ResponseUtil.okList(contentList);
    }

    //endregion

    //region 数据管理--接入记录

    /**
     * 接入记录列表
     */
    @RequiresPermissions("admin:data:authlist")
    @RequiresPermissionsDesc(menu = {"数据管理", "接入记录"}, button = "查询")
    @OperationLog(detail = "查看接入记录信息列表", operationType = OperationType.SELECT, userType = UserType.ADMIN)
    @GetMapping("/authDataList")
    public Object authDataList(@RequestParam(defaultValue = "") String keywords,
                      @RequestParam(defaultValue = "") String area,
                      @RequestParam(defaultValue = "-1") Integer nodeType,
                      @RequestParam(defaultValue = "") String startTime,
                      @RequestParam(defaultValue = "") String endTime,
                      @RequestParam(defaultValue = "1") Integer page,
                      @RequestParam(defaultValue = "10") Integer limit) {
        GeUser userCurrent = getCurrentAdminUser();
        if(StringUtils.isNotBlank(startTime)) {
            startTime += " 00:00:00";
        }
        if(StringUtils.isNotBlank(endTime)) {
            endTime += " 23:59:59";
        }
        List<NodeVo> nodeVos = nodeService.selectAuthDataList(nodeType, area, keywords, startTime, endTime, page, limit);
        return ResponseUtil.okList(nodeVos);
    }

    /**
     * 当前账号信息授权信息
     *
     */
    @RequiresPermissions("admin:data:authdatadetails")
    @RequiresPermissionsDesc(menu = {"数据管理", "接入记录"}, button = "详情")
    @OperationLog(detail = "查看接入记录信息详情", operationType = OperationType.SELECT, userType = UserType.ADMIN)
    @GetMapping("/authDataDetails")
    public Object authDataDetails(@NotNull Integer id) {
        NodeVo nodeVo = unitService.findAssessByAuthId(id);
        if (nodeVo == null) {
            return ResponseUtil.fail();
        }
        return ResponseUtil.ok(nodeVo);
    }

    //endregion

    //region 数据管理--解析记录

    /**
     * 解析记录列表
     */
    @RequiresPermissions("admin:data:analylist")
    @RequiresPermissionsDesc(menu = {"数据管理", "解析记录"}, button = "查看")
    @OperationLog(detail = "查看解析记录信息列表", operationType = OperationType.SELECT, userType = UserType.ADMIN)
    @GetMapping("/analyDataList")
    public Object analyDataList(@RequestParam(defaultValue = "") String keywords,
                               @RequestParam(defaultValue = "-1") Integer nodeType,
                               @RequestParam(defaultValue = "") String area,
                               @RequestParam(defaultValue = "") String startTime,
                               @RequestParam(defaultValue = "") String endTime,
                               @RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer limit) {
        GeUser userCurrent = getCurrentAdminUser();
        if(StringUtils.isNotBlank(startTime)) {
            startTime += " 00:00:00";
        }
        if(StringUtils.isNotBlank(endTime)) {
            endTime += " 23:59:59";
        }
        List<AnalyRecordVo> analyRecordVoList = nodeService.selectAnalyRecordList(nodeType, area, keywords, startTime, endTime, page, limit);
        return ResponseUtil.okList(analyRecordVoList);
    }

    //endregion

    //region 数据管理--接入记录

    /**
     * 数据上报列表
     */
    @RequiresPermissions("admin:data:escalationlist")
    @RequiresPermissionsDesc(menu = {"数据管理", "数据上报"}, button = "查询")
    @OperationLog(detail = "查看数据上报信息列表", operationType = OperationType.SELECT, userType = UserType.ADMIN)
    @GetMapping("/escalationDataList")
    public Object escalationDataList(@RequestParam(defaultValue = "") String keywords,
                                     @RequestParam(defaultValue = "-1") Integer isEscalation,
                                     @RequestParam(defaultValue = "") String startTime,
                                     @RequestParam(defaultValue = "") String endTime,
                                     @RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "10") Integer limit) {
        GeUser userCurrent = getCurrentAdminUser();
        if(StringUtils.isNotBlank(startTime)) {
            startTime += " 00:00:00";
        }
        if(StringUtils.isNotBlank(endTime)) {
            endTime += " 23:59:59";
        }
        List<NodeApplyVo> nodeVos = nodeService.selectEscalationDataList(isEscalation, keywords, startTime, endTime, page, limit);
        return ResponseUtil.okList(nodeVos);
    }
//    endregion

}
